package ModelLayer;

public class LineItem
{
    private int quantity,idP;
    private double subtotal=0.00;
    private Product product;
    private static ProductList products = ProductList.getInstance();
    
    public LineItem(int id,int quantity)
    {
        this.quantity = quantity;
        this.idP = id;
        updateSubTotal();
    }
    
    private void updateSubTotal()
    {
        product = getProductFromId(idP);
        try{
            double productPrice = product.getFinalPrice();
            this.subtotal = quantity * productPrice;
            product.updateStock();
        }
        catch(Exception e)
        {
            this.subtotal = 0.00;
            System.err.println("The product with id "+idP+" does not exist.");
        }
    }
    
    private Product getProductFromId(int id) {  return products.getProduct(id); }
    
    public Product getProduct() { return this.product; }
    
    public boolean availProd() { return (product!=null); }
    
    public double getSubtotal() { return this.subtotal; }
    
    public int getQuantity() { return this.quantity; }
    
    public void setQuantity(int quantity)
    { 
        this.quantity = quantity;
        updateSubTotal();
    }
}
